package com.shyroke.daydayzhuanapi.mapper;

import com.shyroke.daydayzhuanapi.dto.AdminUserDto;
import com.shyroke.daydayzhuanapi.entity.RoleEntity;
import com.shyroke.daydayzhuanapi.entity.UserEntity;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class UserMapper {
    @Autowired
    private ModelMapper modelMapper;

    public AdminUserDto entityToDto(UserEntity userEntity) {
        AdminUserDto dto = modelMapper.map(userEntity, AdminUserDto.class);
        Set<RoleEntity> roleList = userEntity.getRoleList();
        Boolean isAdmin = isAdmin2(roleList);
        if(isAdmin){
            dto.setRoleName("管理员");
        }else{
            dto.setRoleName("投稿用户");
        }
        return dto;
    }

    /**
     * 是否管理员
     * @param roleList
     * @return
     */
    private Boolean isAdmin2(Set<RoleEntity> roleList) {
        boolean flag = false;
        for(RoleEntity role:roleList){
            if("admin".equals(role.getName())){
                flag = true;
            }
        }
        return flag;
    }
}
